package classificationlogic

import (
	"common/comCopier"
	"common/dao/model"
	comJwt "common/jwt"
	"common/mysql"
	"context"
	"fmt"

	"service/internal/svc"
	"service/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *pb.SysFileRequest) (*pb.SysClassificationRespList, error) {
	// todo: add your logic here and delete this line
	user, err := comJwt.BackUser(l.ctx, l.svcCtx.Config.USERDATA)
	if err != nil {
		return nil, err
	}
	fmt.Println(user)
	var (
		resp = pb.SysClassificationRespList{}
	)
	params := mysql.QueryList{
		Page:    int(in.Page),
		Size:    int(in.PageSize),
		Key:     "name",
		FindKey: in.Keyword,
		DB:      l.svcCtx.DB,
	}
	list, count, err := mysql.GETList[model.Classification](&params)
	if err != nil {
		return nil, err
	}
	//b, err := json.Marshal(list)

	//err = utils.RspJsonMarshal(&resp.List, list)
	//if err != nil {
	//	return nil, err
	//}
	err = comCopier.CopyWithOption(&resp.List, list)
	if err != nil {
		return nil, err
	}
	resp.Total = int32(count)
	return &resp, nil
}
